<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;

class ParamType extends Model
{
    protected $fillable = ['name', 'count'];

    protected static function boot()
    {
        parent::boot();
        static::saved(function ($param) {
            // 更新缓存数据
            Cache::forever('params', ParamType::with('params')->get());
        });
    }

    public function params()
    {
        return $this->hasMany(ParamValue::class, 'type_id');
    }

    public function updateCount()
    {
        $this->update(['count' => $this->params->count()]);
    }
}
